#include "camera.h"
#include "..\math\collision.h"

#include <cmath>

namespace ray_tracing
{

	camera_t::camera_t(float in_angle, float in_N, 
		const pnt3f in_eye, const vec3f in_up, const vec3f in_look_at)
		: angle(in_angle),
		  N(in_N),
		  eye(in_eye),
		  up(in_up),
		  look_at(in_look_at)
	{
	}

	ray3f camera_t::get_ray(const float i, const float j, const float ar) const
	{
		struct near_plane_t
		{
			float width;
			float height;
		};

		float W = N*tan(angle/2.f);
		float H = ar*W;
	
		near_plane_t near_plane = { W, H };

		float xp = near_plane.width*(2.f*i - 1.f);
		float yp = near_plane.height*(2.f*j - 1.f);

		vec3f n = normalize(eye-look_at-pnt3f());	
		vec3f u = normalize(cross(up,n));
		vec3f v = normalize(cross(n,u));

		vec3f dir = -N*n + xp*u + yp*v;
		dir = normalize(dir);

		return ray3fc(eye, dir);
	}

}